package com.cx.utils;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: TimeTask
 * @Description:定时任务
 * @Author: cx
 * @Date: 2024/1/31 16:11
 **/
public class TimeTask {
    //属于javaUtil包,在spring框架中可以配合 @PostConstruct使用
    public static void backUpMysql01() {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        Runnable task = () -> {
            //业务
            System.out.println("六小时过去咯" + new Date().getTime());
        };
        //五小时执行一次
        executorService.scheduleAtFixedRate(task, 0, 6, TimeUnit.HOURS);
    }


    //  时间转化为字符串
    public static String timeToString(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = formatter.format(date);
        return strDate;
    }

    public static String dateToString(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = formatter.format(date);
        return strDate;
    }


    public static void main(String[] args) {
        System.out.println(dateToString(new Date()));

    }

}
